package com.yt.product.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.product.models.bo.ShoppingCartSaveBO;
import com.yt.product.models.dto.ProductInfoDTO;
import com.yt.product.models.vo.ShoppingCartAccountVO;
import com.yt.product.models.vo.ShoppingCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.product.models.service.IShoppingCartService;
import com.yt.product.models.bo.ShoppingCartBO;
import com.yt.product.models.dto.ShoppingCartDTO;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车控制器
 *
 * @author lvzeming
 * @Date 2025-08-22 15:52:56
 */

@RestController
@RequestMapping("/api/shoppingCart")
@Slf4j
@Api(value = "ShoppingCartApiController",tags = "购物车Api接口")
public class ShoppingCartApiController {

    @DubboReference
    private IShoppingCartService shoppingCartService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShoppingCartDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShoppingCartDTO dto = shoppingCartService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/addCart")
    @ApiOperation(value = "添加购物车",notes = "添加购物车")
    public Result<Boolean> addCart(@RequestBody ShoppingCartSaveBO saveBO){
        if(saveBO == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        boolean flag = shoppingCartService.saveCart(saveBO);
        return new Result<>(flag);
    }

    @PostMapping("/getCartByUserId")
    @ApiOperation(value = "通过用户id查询购物车",notes = "通过用户id查询购物车")
    @ApiImplicitParam(name = "userId",value = "userId",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShoppingCartVO>> getCartByUserId(@RequestParam("userId") Long userId){
        if(userId == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        List<ShoppingCartVO> shoppingCartVOS = shoppingCartService.queryCartByUserId(userId);
        return new Result<>(shoppingCartVOS);
    }

    @PostMapping("/deleteCartByUserIdAndProductId")
    @ApiOperation(value = "删除购物车",notes = "删除购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "产品ID", required = true, dataType = "Long", paramType = "query")
    })
    public Result<Boolean> deleteCartByUserIdAndProductId(@RequestParam("userId") Long userId,
                                                                       @RequestParam("productId") Long productId){
        if(userId == null || productId == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<ShoppingCartDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("product_id",productId);
        boolean flag = shoppingCartService.delete(wrapper);
        return new Result<>(flag);
    }


    @PostMapping("/mergeCartList")
    @ApiOperation(value = "合并购物车",notes = "合并购物车")
    public Result<Boolean> mergeCartList(@RequestBody List<ShoppingCartSaveBO> list) {

        boolean flag = shoppingCartService.mergeCartList(list);
        return new Result<>(flag);
    }

    @PostMapping("/checkout")
    @ApiOperation(value = "购物车结算",notes = "购物车结算")
    public Result<ShoppingCartAccountVO> checkout(@RequestBody List<ShoppingCartSaveBO> list){
        ShoppingCartAccountVO shoppingCartAccountVO = shoppingCartService.getAccountDetail(list);
        return new Result<>(shoppingCartAccountVO);
    }

    @ApiOperation(value = "购物车列表",notes = "购物车列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShoppingCartDTO>> queryPageList(ShoppingCartBO bo){
        Wrapper<ShoppingCartDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ShoppingCartDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shoppingCartService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
